package com.mc.gpm.service.impl;

import java.math.BigDecimal;
import java.util.List;
import com.mc.common.utils.DateUtils;
import com.mc.gpm.domain.GpmStorage;
import com.mc.gpm.mapper.GpmStorageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mc.gpm.mapper.GpmOutStorageMapper;
import com.mc.gpm.domain.GpmOutStorage;
import com.mc.gpm.service.IGpmOutStorageService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 种质资源出库Service业务层处理
 *
 * @author mc
 * @date 2021-03-19
 */
@Service
public class GpmOutStorageServiceImpl implements IGpmOutStorageService
{
    @Autowired
    private GpmOutStorageMapper gpmOutStorageMapper;
    @Autowired
    private GpmStorageMapper storageMapper;

    /**
     * 查询种质资源出库
     *
     * @param id 种质资源出库ID
     * @return 种质资源出库
     */
    @Override
    public GpmOutStorage selectGpmOutStorageById(String id)
    {
        return gpmOutStorageMapper.selectGpmOutStorageById(id);
    }

    /**
     * 查询种质资源出库列表
     *
     * @param gpmOutStorage 种质资源出库
     * @return 种质资源出库
     */
    @Override
    public List<GpmOutStorage> selectGpmOutStorageList(GpmOutStorage gpmOutStorage)
    {
        return gpmOutStorageMapper.selectGpmOutStorageList(gpmOutStorage);
    }

    /**
     * 新增种质资源出库
     *
     * @param gpmOutStorage 种质资源出库
     * @return 结果
     */
    @Transactional//多次插入开启事务
    @Override
    public int insertGpmOutStorage(GpmOutStorage gpmOutStorage)
    {
        gpmOutStorage.setCreateTime(DateUtils.getNowDate());
        GpmStorage gpmStorage = storageMapper.selectGpmStorageById(gpmOutStorage.getStorageId());
        if((gpmOutStorage.getOutAmount().compareTo(gpmStorage.getAmount()))==-1){
            gpmStorage.setAmount(gpmStorage.getAmount().subtract(gpmOutStorage.getOutAmount()));
        }else{
            gpmStorage.setAmount(BigDecimal.valueOf(0));
            gpmStorage.setStatus("1");
        }
        gpmStorage.setOutCount(gpmStorage.getOutCount()+1);
        storageMapper.updateGpmStorage(gpmStorage);
        return gpmOutStorageMapper.insertGpmOutStorage(gpmOutStorage);
    }

    /**
     * 修改种质资源出库
     *
     * @param gpmOutStorage 种质资源出库
     * @return 结果
     */
    @Override
    public int updateGpmOutStorage(GpmOutStorage gpmOutStorage)
    {
        return gpmOutStorageMapper.updateGpmOutStorage(gpmOutStorage);
    }


}